﻿using System;
using System.Runtime.InteropServices;
using System.Security;

namespace AwesomiumDotNet
{
    /// <summary>
	/// An enumeration of the three verbosity settings for the Awesomium Log.
	/// </summary>
	public enum LogLevel
	{
		/// <summary> No log is created </summary>
		None,
		/// <summary> Logs only errors </summary>
		Normal,
		/// <summary> Logs everything </summary>
		Verbose
	};

    /// <summary>
    /// Configuration settings for the WebCore.
    /// </summary>
    public class WebCoreConfig
    {
        /// <summary>
        /// Creates a default WebCoreConfig with the following default settings:
        /// <para>EnablePlugins           = false</para>
        /// <para>UserDataPath            = (empty)</para>
        /// <para>PluginPath              = (empty)</para>
        /// <para>LogPath                 = (empty)</para>
        /// <para>LogLevel                = Normal</para>
        /// <para>UserAgentOverride       = (empty)</para>
        /// <para>ProxyServer             = "none"</para>
        /// <para>ProxyConfigScript       = (empty)</para>
        /// <para>SaveCacheAndCookies     = true</para>
        /// <para>MaxCacheSize            = 0</para>
        /// <para>DisableSameOriginPolicy = false</para>
        /// <para>CustomCss               = (empty)</para>
        /// </summary>
        public WebCoreConfig()
        {
            unsafe
            {
                unmanaged = (__WebCoreConfig*)Memory.Alloc(sizeof(__WebCoreConfig));
                WebCoreConfig_Ctor(unmanaged);
            }
        }

        /// <summary>
        /// Set whether or not Javascript is enabled.
        /// </summary>
        public void SetEnableJavaScript(bool enablePlugins)
        {
            unsafe
            {
                WebCoreConfig_SetEnableJavaScript(unmanaged, enablePlugins);
            }
        }

        /// <summary>
        /// Set whether or not to enable embedded plugins (e.g., Flash).
        /// </summary>
        public void SetEnablePlugins(bool enablePlugins)
        {
            unsafe
            {
                WebCoreConfig_SetEnablePlugins(unmanaged, enablePlugins);
            }
        }

        /// <summary>
        /// Set a string of custom CSS to be included as the global default
		/// style for all pages. This is especially useful for customizing
		/// scrollbars and other look-and-feel elements.
        /// </summary>
        public void SetCustomCss(string customCss)
        {
            unsafe
            {
                astring customCssTemp = new astring(customCss);
                WebCoreConfig_SetCustomCss(unmanaged, &customCssTemp);
                customCssTemp.Free();
            }
        }

        /// <summary>
        /// Set a string of custom CSS to be included as the global default
        /// style for all pages. This is especially useful for customizing
        /// scrollbars and other look-and-feel elements.
        /// </summary>
        public void SetCustomCssFromFile(string customCssFilePath)
        {
            unsafe
            {
                astring customCssFilePathTemp = new astring(customCssFilePath);
                WebCoreConfig_SetCustomCssFromFile(unmanaged, &customCssFilePathTemp);
                customCssFilePathTemp.Free();
            }
        }

        /// <summary>
        /// Set whether or not the "same-origin" policy should be disabled.
        /// See http://en.wikipedia.org/wiki/Same_origin_policy
        /// </summary>
        public void SetDisableSameOriginPolicy(bool disableSameOriginPolicy)
        {
            unsafe
            {
                WebCoreConfig_SetDisableSameOriginPolicy(unmanaged, disableSameOriginPolicy);
            }
        }

        /// <summary>
        /// Set the logging level to use, this can be either None,
        /// Normal, or Verbose.
        /// </summary>
        public void SetLogLevel(LogLevel logLevel)
        {
            unsafe
            {
                WebCoreConfig_SetLogLevel(unmanaged, (int)logLevel);
            }
        }

		/// <summary>
		/// Set the path to store the awesomium.log. If none is specified, the
		/// log will be stored in the working directory.
		/// </summary>
		public void SetLogPath(String logPath)
        {
            unsafe
            {
                wstring logPathTemp = new wstring(logPath);
                WebCoreConfig_SetLogPath(unmanaged, &logPathTemp);
                logPathTemp.Free();
            }
        }

        /// <summary>
        /// Set the maximum disk space to be used by the disk cache, in bytes.
        /// Specify 0 to use no limit.
        /// </summary>
        public void SetMaxCacheSize(int maxCacheSize)
        {
            unsafe
            {
                WebCoreConfig_SetMaxCacheSize(unmanaged, maxCacheSize);
            }
        }

        /// <summary>
        /// Set an absolute path that will be included in the search for plugins. 
        /// This is useful if you wish to bundle certain plugins with your 
        /// application.
        /// </summary>
        public void SetPluginPath(String pluginPath)
        {
            unsafe
            {
                wstring pluginPathTemp = new wstring(pluginPath);
                WebCoreConfig_SetPluginPath(unmanaged, &pluginPathTemp);
                pluginPathTemp.Free();
            }
        }

        /// <summary>
        /// Set the path to the directory that will be used to store cache, 
		/// cookies, and other data. If an empty string is specified, this path 
		/// defaults to "./Default".
        /// </summary>
        public void SetUserDataPath(String userDataPath)
        {
            unsafe
            {
                wstring userDataPathTemp = new wstring(userDataPath);
                WebCoreConfig_SetUserDataPath(unmanaged, &userDataPathTemp);
                userDataPathTemp.Free();
            }
        }

        // Note: don't use bool in this struct, such marshalling is not supported.
        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        internal unsafe struct __WebCoreConfig
        {
            byte enablePlugins;
            byte enableJavaScript;
            byte pad0;
            byte pad1;
            wstring userDataPath;
            wstring pluginPath;
            wstring logPath;
            int logLevel;
            astring userAgentOverride;
            astring proxyServer;
            astring proxyConfigScript;
            int saveCacheAndCookies;
            int maxCacheSize;
            int disableSameOriginPolicy;
            astring customCSS;
        }

        internal unsafe __WebCoreConfig* unmanaged;

        [DllImport("Awesomium", EntryPoint = "??0WebCoreConfig@Awesomium@@QAE@XZ", CallingConvention = CallingConvention.ThisCall), SuppressUnmanagedCodeSecurityAttribute]
        unsafe extern static void WebCoreConfig_Ctor(__WebCoreConfig* inst);

        [DllImport("Awesomium", EntryPoint = "??0WebCoreConfig@Awesomium@@QAE@ABV01@@Z", CallingConvention = CallingConvention.ThisCall), SuppressUnmanagedCodeSecurityAttribute]
        unsafe extern static void WebCoreConfig_Ctor(__WebCoreConfig* inst, __WebCoreConfig* inst2);

        [DllImport("Awesomium", EntryPoint = "??1WebCoreConfig@Awesomium@@QAE@XZ", CallingConvention = CallingConvention.ThisCall), SuppressUnmanagedCodeSecurityAttribute]
        unsafe extern static void WebCoreConfig_Dtor(__WebCoreConfig* inst);

        [DllImport("Awesomium", EntryPoint = "??4WebCoreConfig@Awesomium@@QAEAAV01@ABV01@@Z", CallingConvention = CallingConvention.ThisCall), SuppressUnmanagedCodeSecurityAttribute]
        unsafe extern static __WebCoreConfig* WebCoreConfig_Equal(__WebCoreConfig* inst);

        [DllImport("Awesomium", EntryPoint = "?setEnableJavascript@WebCoreConfig@Awesomium@@QAEX_N@Z", CallingConvention = CallingConvention.ThisCall), SuppressUnmanagedCodeSecurityAttribute]
        unsafe extern static void WebCoreConfig_SetEnableJavaScript(__WebCoreConfig* inst, bool enableJavaScript);

        [DllImport("Awesomium", EntryPoint = "?setEnablePlugins@WebCoreConfig@Awesomium@@QAEX_N@Z", CallingConvention = CallingConvention.ThisCall), SuppressUnmanagedCodeSecurityAttribute]
        unsafe extern static void WebCoreConfig_SetEnablePlugins(__WebCoreConfig* inst, bool enablePlugins);

        [DllImport("Awesomium", EntryPoint = "?setCustomCSS@WebCoreConfig@Awesomium@@QAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z", CallingConvention = CallingConvention.ThisCall), SuppressUnmanagedCodeSecurityAttribute]
        unsafe extern static void WebCoreConfig_SetCustomCss(__WebCoreConfig* inst, astring* customCss);

        [DllImport("Awesomium", EntryPoint = "?setCustomCSSFromFile@WebCoreConfig@Awesomium@@QAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z", CallingConvention = CallingConvention.ThisCall), SuppressUnmanagedCodeSecurityAttribute]
        unsafe extern static void WebCoreConfig_SetCustomCssFromFile(__WebCoreConfig* inst, astring* customCssFilePath);

        [DllImport("Awesomium", EntryPoint = "?setDisableSameOriginPolicy@WebCoreConfig@Awesomium@@QAEX_N@Z", CallingConvention = CallingConvention.ThisCall), SuppressUnmanagedCodeSecurityAttribute]
        unsafe extern static void WebCoreConfig_SetDisableSameOriginPolicy(__WebCoreConfig* inst, bool disableSameOriginPolicy);

        [DllImport("Awesomium", EntryPoint = "?setLogLevel@WebCoreConfig@Awesomium@@QAEXW4LogLevel@2@@Z", CallingConvention = CallingConvention.ThisCall), SuppressUnmanagedCodeSecurityAttribute]
        unsafe extern static void WebCoreConfig_SetLogLevel(__WebCoreConfig* inst, int logLevel);

        [DllImport("Awesomium", EntryPoint = "?setLogPath@WebCoreConfig@Awesomium@@QAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z", CallingConvention = CallingConvention.ThisCall), SuppressUnmanagedCodeSecurityAttribute]
        unsafe extern static void WebCoreConfig_SetLogPath(__WebCoreConfig* inst, astring* logPath);

        [DllImport("Awesomium", EntryPoint = "?setLogPath@WebCoreConfig@Awesomium@@QAEXABV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@@Z", CallingConvention = CallingConvention.ThisCall), SuppressUnmanagedCodeSecurityAttribute]
        unsafe extern static void WebCoreConfig_SetLogPath(__WebCoreConfig* inst, wstring* logPath);

        [DllImport("Awesomium", EntryPoint = "?setMaxCacheSize@WebCoreConfig@Awesomium@@QAEXH@Z", CallingConvention = CallingConvention.ThisCall), SuppressUnmanagedCodeSecurityAttribute]
        unsafe extern static void WebCoreConfig_SetMaxCacheSize(__WebCoreConfig* inst, int maxCacheSize);

        [DllImport("Awesomium", EntryPoint = "?setPluginPath@WebCoreConfig@Awesomium@@QAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z", CallingConvention = CallingConvention.ThisCall), SuppressUnmanagedCodeSecurityAttribute]
        unsafe extern static void WebCoreConfig_SetPluginPath(__WebCoreConfig* inst, astring* pluginPath);

        [DllImport("Awesomium", EntryPoint = "?setPluginPath@WebCoreConfig@Awesomium@@QAEXABV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@@Z", CallingConvention = CallingConvention.ThisCall), SuppressUnmanagedCodeSecurityAttribute]
        unsafe extern static void WebCoreConfig_SetPluginPath(__WebCoreConfig* inst, wstring* pluginPath);

        [DllImport("Awesomium", EntryPoint = "?setUserDataPath@WebCoreConfig@Awesomium@@QAEXABV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@@Z", CallingConvention = CallingConvention.ThisCall), SuppressUnmanagedCodeSecurityAttribute]
        unsafe extern static void WebCoreConfig_SetUserDataPath(__WebCoreConfig* inst, wstring* userDataPath);
    }
}
